package threadPoolTest;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @SingleThreadExecutor: 单线程的线程池，线程异常结束，会创建一个新的线程，能确保任务按提交顺序执行
 * @author: ZBoHang
 * @time: 2021/9/6 17:36
 */
public class SingleThreadExecutor {

    static ExecutorService singleExec = Executors.newSingleThreadExecutor();

    public static void main(String[] args) {
        test();
    }

    private static void test() {
        for (int i = 0; i < 6; i++) {
            final int index = i;
            singleExec.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + " index--> " + index);
                }
            });
        }
    }
}
